<?php
/**
 * Date: 2018/6/26
 * Time: 14:04
 * Author: zeronlee
 */

namespace App\Models\SysConfig;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;

class SysConfig extends Model
{

    protected $table = 'sys_config';

    protected $primaryKey = 'id';

    protected $fillable = [];

    protected $guarded = [];

    public $timestamps = true;

    /**
     * 设置一项
     * @param mixed $request
     * @return bool
     * @throws \Exception
     */
    public function setRow($request){
        switch (intval($request->type)){
            case 1:
                $module = 'base';

                $setting = $request->sysconfig['base'];
                break;
            case 2:
                $module = 'email';
                $setting = $request->sysconfig['email'];
                break;
            case 3:
                $module = 'distribute';
                $setting = $request->sysconfig['distribute'];
                break;
            default:
                $module = '';
                $setting = [];

        }
        $data = ['module'=>$module,'name'=>$module,'setting'=>json_encode($setting),'disabled'=>0];
        $where = ['module'=>$module];
        try{

            if ($this->where($where)->first()){
                //update
                return $this->updateRow($where,$data);
            } else{
                //insert
                return $this->addRow($data);
            }
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 新增一项设置
     * @param array $data
     * @return bool
     * @throws \Exception
     */
    public function addRow ($data){
        if (is_array($data)){

            if ($this->create($data)) {

                return true;
            }
        }
        throw new \Exception('添加失败');
    }

    /**
     * 更新一项设置
     * @param array $where
     * @param array $data
     * @return bool
     * @throws \Exception
     */
    public function updateRow ($where,$data){
        if (is_array($where) && is_array($data)){

            if ($this->where($where)->update($data) > 0){
                return true;
            }
        }
        throw new \Exception('修改失败');

    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function baseRules()
    {
        return [
            'name' => 'required|max:255',
            'address' => 'required|url'
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function baseMessages()
    {
        return [
            'name.required' => '请填写站点名',
            'address.required'  => '请填写网站地址',
            'address.url'  => '请填写合法的网站地址'
        ];
    }
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function emailRules()
    {
        return [
            /*'driver' => 'required',
            'server' => 'required',
            'port' => 'required|numeric',*/
            'rcp_type' => 'required|numeric',
            'rcp_default' => 'required_if:rcp_type,1',
            'istimeout.serious' => 'required|numeric',
            'istimeout.ishighrisk' => 'required|numeric',
            'istimeout.low' => 'required|numeric',
            'hastimeout.serious' => 'required|numeric',
            'hastimeout.ishighrisk' => 'required|numeric',
            'hastimeout.low' => 'required|numeric'
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function emailMessages()
    {
        return [
            /*'driver.required' => '请填写发送模式',
            'server.required'  => '请填写邮件服务器',
            'port.required'  => '请填写邮件服务器端口',
            'port.numeric'  => '请填写合法的端口号',*/
            'rcp_type.required'  => '请填写发送类型',
            'rcp_type.numeric'  => '请填写数字',
            'rcp_default.required_if'  => '请填写默认收件人',
            'istimeout.serious.required'  => '请填写即将超时天数',
            'istimeout.serious.numeric'  => '请填写合法的天数',
            'istimeout.ishighrisk.required'  => '请填写即将超时天数',
            'istimeout.ishighrisk.numeric'  => '请填写合法的天数',
            'istimeout.low.required'  => '请填写即将超时天数',
            'istimeout.low.numeric'  => '请填写合法的天数',
            'hastimeout.serious.required'  => '请填写超时天数',
            'hastimeout.serious.numeric'  => '请填写合法的天数',
            'hastimeout.ishighrisk.required'  => '请填写超时天数',
            'hastimeout.ishighrisk.numeric'  => '请填写合法的天数',
            'hastimeout.low.required'  => '请填写超时天数',
            'hastimeout.low.numeric'  => '请填写合法的天数'
        ];
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function distributeRules()
    {
        return [
            'start_time' => 'required|date_format:m/d/Y',
            'end_time'   => 'required|date_format:m/d/Y|after:start_time'
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function distributeMessages()
    {
        return [
            'start_time.required' => '请选择开始时间',
            'start_time.date_format' => '请选择合法的时间格式',
            'end_time.required'  => '请选择结束时间',
            'end_time.date_format' => '请选择合法的时间格式',
            'end_time.after' => '请选择大于开始的时间'
        ];
    }

    /**
     * validate base
     *
     * @param array $data
     *
     * @return mixed
     */
    public function vlidateBase($data){
        return Validator::make($data,
            $this->baseRules(),
            $this->baseMessages()
        );
    }

    /**
     * validate email
     *
     * @param array $data
     *
     * @return mixed
     */
    public function vlidateEmail($data){
        return Validator::make($data,
            $this->emailRules(),
            $this->emailMessages()
        );
    }
    /**
     * validate distribute
     *
     * @param array $data
     *
     * @return mixed
     */
    public function vlidateDistribute($data){
        return Validator::make($data,
            $this->distributeRules(),
            $this->distributeMessages()
        );
    }

}